<?php

/**
 * Viewing utilities.
 *
 * @author Ian Clarence <ian.clarence@gmail.com>
 */
class ViewUtils {

    /**
     * The constructor
     */
    public function __construct() {
    }

    /**
     * Displaying a brief summary of an object. $url is not appropriate for all.
     * 
     * @param int $id
     * @param string $name
     * @param string $url
     * @return ViewHtmlElement
     */
    public static function viewSummary($id, $name, $url = NULL) {
        $output = ViewHtmlElement::elementInit("p", "{$id}: {$name}: ");
        if ($url != NULL) {
            $link = ViewHtmlElement::elementInit("a", $url, "href", $url);
            $output->addInnerHtml($link);
        }
        return $output;
    }
    
    /**
     * Getting the panel types as an array.
     * 
     * @return array
     */
    public static function getPanelTypes() {
        $results = PanelType::getList();
        $output = array();
        foreach ($results as $result) {
            $output[$result['id']] = $result['name'];
        }
        return $output;
    }
    
    /**
     * Getting the article types as an array.
     * 
     * @return array
     */
    public static function getArticleTypes() {
        $results = ArticleType::getList();
        $output = array();
        foreach ($results as $result) {
            $output[$result['id']] = $result['name'];
        }
        return $output;
    }
    
    /**
     * Getting the medium types as an array.
     * 
     * @return array
     */
    public static function getMediumTypes() {
        $results = MediumType::getList();
        $output = array();
        foreach ($results as $result) {
            $output[$result['id']] = $result['name'];
        }
        return $output;
    }
    
    /**
     * Getting the form elements types as an array.
     * 
     * @return array
     */
    public static function getHtmlFormElementTypes() {
        $results = HtmlFormElementType::getList();
        $output = array();
        foreach ($results as $result) {
            $output[$result['id']] = $result['name'];
        }
        return $output;
    }
    
    /**
     * Returning a string in the CSS giving a number of pixels: rounds to the nearest pixel e.g. '100px'.
     * 
     * @param mixed $numberOfPixels
     * @return string
     */
    public static function px($numberOfPixels) {
        return sprintf('%01d', round($numberOfPixels)) . 'px';
    }
    
    /**
     * Returning a string in the CSS giving an EM value e.g. '1.5em'.
     * Converts number of pixels to EMs.
     * 
     * @param mixed $numberOfPixels
     * @return string
     */
    public static function em($numberOfPixels) {
        return sprintf('%01.3f', $numberOfPixels / BODY_FONT_SIZE) . 'em';
    }
    
    /**
     * Adding a component to a URL: if the last character is a slash, do not add another one.
     * 
     * @param string $url
     * @param string $added
     * @return string
     */
    public static function addUrlComponent($url, $added) {
        return substr($url, -1) == '/' ? $url . $added : $url . '/' . $added;
    }

}
